<template>
	<view class="page-row">
		<view class="address-box">
			<view class="form-box">
				<view>
					<text>收货人</text>
					<view>
						<input placeholder="请输入" v-model="postData.username" />
					</view>
				</view>
				<view>
					<text></text>
					<view>
						<u-radio-group v-model="postData.gender" active-color="#1B3893">
							<u-radio name="先生">先生</u-radio>
							<u-radio name="女士">女士</u-radio>
						</u-radio-group>
					</view>
				</view>
				<view>
					<text>手机号码</text>
					<view>
						<input placeholder="请输入" v-model="postData.telephone" :maxlength="11" />
					</view>
				</view>
				<view>
					<text>选择地区</text>
					<view @click="openMaps">
						<input placeholder="点击选择" disabled />
						<view class="set-pdf">
							<u-icon name="arrow-right" color="#9B9B9B" size="14" ></u-icon>
						</view>
					</view>
				</view>
				<view class="colums">
					<view>
						<textarea placeholder="详细地址：如街道、楼牌号等~~" v-model="postData.address" />
					</view>
				</view>
				<view>
					<text>设为默认地址</text>
					<view>
						<u-switch v-model="postData.is_default" activeColor="#023F92"></u-switch>
					</view>
				</view>
			</view>
			<view class="btn-boxVer">
				<view hover-class="hover_class" @click="save">保存</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		addressSave
	} from 'utils/api/user.js'
	export default {
		data() {
			return {
				postData: {
					username: '',
					telephone: '',
					address: '',
					coordinate: '',
					is_default: false,
					gender: '先生'
				},
				ruleToast: {
					username: '请输入用户名',
					telephone: '请输入手机号码',
					address: '请输入详细地址'
				}
			}
		},
		onLoad(data) {
			if(data.id) {
				this.getDetails()
				uni.setNavigationBarTitle({
					title: "编辑收货地址"
				})
			}
		},
		methods: {
			// 选择所在地区
			openMaps() {
				uni.chooseLocation({
					success: (res) => {
						this.postData.address = res.address
						this.postData.coordinate = res.longitude + ',' + res.latitude
					},
					fail: function (err) {
						uni.showToast({
							title: err.errMsg,
							icon: 'none'
						})
					}
				  })
			},
			getDetails() {
				this.postData = JSON.parse(uni.getStorageSync('addressDetail'))
				this.postData.is_default = this.postData.is_default == 1 ? false : true
			},
			save() {
				const keys = Object.keys(this.ruleToast)
				for(var i=0; i<keys.length; i++) {
					const item = keys[i]
					if(!this.postData[item]) {
						return uni.showToast({
							title: this.ruleToast[item],
							icon: 'none'
						})
					}
				}
				uni.showLoading({
					title: '保存中',
					mask: true
				})
				const allData = JSON.parse(JSON.stringify(this.postData))
				allData.is_default = allData.is_default ? '2' : '1'
				addressSave(allData).then(() => {
					uni.hideLoading()
					uni.showToast({ title: '保存成功', icon: 'none'})
					setTimeout(() => {
						uni.navigateBack()
					}, 500)
				})
			}
		}
	}
</script>

<style scoped lang="scss">
/deep/ .u-radio-group {
	>view {
		margin-right: 40upx;
		font-size: 26upx;
	}
}
.page-row {
	width: 100%;
	min-height: 100vh;
	background-color: #F6F6F6;
	box-sizing: border-box;
	padding: 32upx 20upx;
}
.set-pdf {
	margin-right: -7upx;
	margin-left: 6upx;
}
.colums {
	flex-direction: column !important;
	align-items: flex-start !important;
	box-sizing: border-box;
	padding: 40upx 0 20upx 0;
	width: 100%;
	>text {
		margin-bottom: 38upx;
	}
	>view {
		width: 100%;
	}
	textarea {
		height: 160upx;
		text-align: left !important;
	}
}
.address-box {
	box-sizing: border-box;
	padding: 0 32upx;
	background-color: #fff;
	border-radius: 20upx;
	.form-box {
		>view {
			border-bottom: 1px solid #F6F6F6;
			display: flex;
			min-height: 110upx;
			align-items: center;
			>text {
				font-size: 26upx; 
				color: #000000;
				margin-right: 40upx;
				width: 170upx;
			}
			>view {
				flex: 1;
				display: flex;
				align-items: center;
				>image {
					width: 24upx;
					height: 26upx;
				}
				input, textarea {
					flex: 1;
					text-align: left;
					font-size: 26upx;
				}
			}
		}
	}
}
</style>